combined_dat <- read_rds("phc_replications.rds")

fixed <- 2
spacing <- .285

##--------Figure B.1---------------
region_dat <-
  combined_dat %>%
  filter(!is.na(x_region)) %>%
  group_by(admin_browser, x_region) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_region)) %>%
      group_by(x_region) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_region)) %>%
      group_by(admin_nonmobile, x_region) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile"))
  )


g <-
  ggplot(region_dat, aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap( ~ x_region) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")

g %>%
  ggsave(
    filename = "appendix_fig_b1.pdf",
    .,
    width = 6.5,
    height = fixed + spacing * (nrow(g$data) * .5)
  )

##--------Figure B.2---------------
edu_dat <-
  combined_dat %>%
  filter(!is.na(x_education)) %>%
  group_by(admin_browser, x_education) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_education)) %>%
      group_by(x_education) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_education)) %>%
      group_by(admin_nonmobile, x_education) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile")),
    x_education = factor(
      x_education,
      levels = c(
        "No high school diploma",
        "High school diploma",
        "Some college",
        "Associate's degree",
        "Bachelor's degree",
        "Graduate degree"
      )
    )
  )


g <-
  ggplot(edu_dat, aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap( ~ x_education) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")

g %>%
  ggsave(
    filename = "appendix_fig_b2.pdf",
    .,
    width = 6.5,
    height = fixed + spacing * (nrow(g$data) * .5)
  )

##--------Figure B.3---------------
income_dat <-
  combined_dat %>%
  filter(!is.na(x_hhi), x_hhi != "Unanswered") %>%
  group_by(admin_browser, x_hhi) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_hhi), x_hhi != "Unanswered") %>%
      group_by(x_hhi) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_hhi), x_hhi != "Unanswered") %>%
      group_by(admin_nonmobile, x_hhi) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    x_hhi = factor(
      x_hhi,
      levels = c(
        "$19,999 or less",
        "$20,000 - $34,999",
        "$35,000 - $49,999",
        "$50,000 - $64,999",
        "$65,000 - $79,999",
        "$80,000 - $99,999",
        "$100,000 - $124,999",
        "$125,000 - $199,999",
        "$200,000 and above"
      )
    ),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile"))
  )


g <-
  ggplot(income_dat, aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap( ~ x_hhi) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")


g %>%
  ggsave(
    filename = "appendix_fig_b3.pdf",
    .,
    width = 6.5,
    height = fixed + spacing * (nrow(g$data) * .5)
  )

##--------Figure B.4---------------
age_dat <-
  combined_dat %>%
  filter(!is.na(x_age)) %>%
  group_by(admin_browser, x_age) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_age)) %>%
      group_by(x_age) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_age)) %>%
      group_by(admin_nonmobile, x_age) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile")),
    x_age = factor(
      x_age,
      levels = c("18-23",
                 "24-29",
                 "30-39",
                 "40-49",
                 "50-59",
                 "60-69",
                 "70+")
    )
  )


g <-
  ggplot(age_dat, aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap( ~ x_age, nrow = 3) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")

g %>%
  ggsave(
    filename = "appendix_fig_b4.pdf",
    .,
    width = 6.5,
    height = fixed + spacing * (nrow(g$data) * .5)
  )

##--------Figure B.5---------------
gender_dat <-
  combined_dat %>%
  filter(!is.na(x_sex)) %>%
  group_by(admin_browser, x_sex) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_sex)) %>%
      group_by(x_sex) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_sex)) %>%
      group_by(admin_nonmobile, x_sex) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile"))
  )


g <-
  ggplot(gender_dat,
         aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap(~ x_sex) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")

g %>%
  ggsave(
    filename = "appendix_fig_b5.pdf",
    .,
    width = 6.5,
    height = fixed + spacing * (nrow(g$data) * .5)
  )

##--------Figure B.6---------------
race_dat <-
  combined_dat %>%
  filter(!is.na(x_race)) %>%
  group_by(admin_browser, x_race) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_race)) %>%
      group_by(x_race) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_race)) %>%
      group_by(admin_nonmobile, x_race) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile")),
    x_race = factor(
      x_race,
      levels = c("AAPI", "Black",
                 "Hispanic",
                 "White",
                 "Other race"),
      labels = c("AAPI", "Black",
                 "Hispanic",
                 "White",
                 "Other")
    )
  )


g <-
  ggplot(race_dat, aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap( ~ x_race, nrow = 3) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")

g %>%
  ggsave(
    filename = "appendix_fig_b6.pdf",
    .,
    width = 6.5,
    height = fixed + spacing * (nrow(g$data) * .5)
  )


##--------Figure B.7---------------
party_dat <-
  combined_dat %>%
  filter(!is.na(x_pid3)) %>%
  group_by(admin_browser, x_pid3) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_pid3)) %>%
      group_by(x_pid3) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_pid3)) %>%
      group_by(admin_nonmobile, x_pid3) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile")),
    x_pid3 = factor(x_pid3, levels = c("Democrat",
                                       "Republican",
                                       "Independent"))
  )


g <-
  ggplot(party_dat, aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap( ~ x_pid3, nrow = 3) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")

g %>%
  ggsave(
    filename = "appendix_fig_b7.pdf",
    .,
    width = 6.5,
    height = 4 + spacing * (nrow(g$data) * .5)
  )


##--------Figure B.8---------------
vote_dat <-
  combined_dat %>%
  filter(!is.na(x_vote16)) %>%
  group_by(admin_browser, x_vote16) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n),
         sample = ifelse(admin_browser == 0, "Web Apps", "Browsers")) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_vote16)) %>%
      group_by(x_vote16) %>%
      summarise(n = n()) %>%
      mutate(pct = n / sum(n),
             sample = "Full sample")
  ) %>%
  bind_rows(
    combined_dat %>%
      filter(!is.na(x_vote16)) %>%
      group_by(admin_nonmobile, x_vote16) %>%
      summarise(n = n()) %>%
      mutate(
        pct = n / sum(n),
        sample = ifelse(admin_nonmobile == 0, "Mobiles", "Non-Mobiles")
      )
  ) %>%
  mutate(
    sample = factor(sample, levels = rev(
      c("Full sample", "Browsers",
        "Web Apps", "Mobiles",
        "Non-Mobiles")
    )),
    std.error = sqrt((pct * (1 - pct)) / n),
    conf.low = pct - 1.96 * std.error,
    conf.high = pct + 1.96 * std.error,
    type = case_when(
      sample == "Full sample" ~ "Full",
      sample %in% c("Browsers", "Web Apps") ~ "Browser",
      sample %in% c("Mobiles", "Non-Mobiles") ~ "Mobile"
    ),
    type = factor(type, levels = c("Full", "Browser", "Mobile")),
    x_vote16 = factor(x_vote16, levels = c("Clinton", "Trump", "No vote",
                                           "Other vote"))
  )


g <-
  ggplot(vote_dat,
         aes(x = pct, color = type, y = sample)) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    height = 0,
    size = 1,
    position = position_dodge(width = 0.9)
  ) +
  geom_point(size = 2.5) +
  facet_wrap(~ x_vote16) +
  theme_ycls() +
  scale_x_continuous(labels = scales::label_percent(accuracy = 1)) +
  scale_color_manual("", values = c("red", "black", "darkgrey")) +
  theme(
    panel.grid.major.x = element_blank(),
    axis.ticks.y = element_line(size = 0.2,  color = "lightgrey"),
    panel.grid.major.y = element_line(size = 0.2, color = "lightgrey")
  ) +
  labs(title = "",
       x = "Proportion of characteristic in sample",
       y = "")

g %>%
  ggsave(
    filename = "appendix_fig_b8.pdf",
    .,
    width = 6.5,
    height = fixed + spacing * (nrow(g$data) * .5)
  )